package cn.itsource.template;

import cn.itsource.constants.GlobalExceptionCode;
import cn.itsource.exception.GlobalException;
import cn.itsource.pojo.app.bo.AccesstokenResult;
import cn.itsource.pojo.app.bo.OpenIdResult;

import cn.itsource.pojo.app.bo.PhoneInfo;
import cn.itsource.pojo.app.bo.PhoneResult;
import cn.itsource.pojo.properties.WechatProperties;
import cn.itsource.utils.AssertUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * 📖：beishan
 * 🗓️：2024/3/29 16:26
 * =====================
 * 微信工具
 */
@Slf4j
public class WechatTemplate {
    private WechatProperties wechatProperties;
    private RestTemplate restTemplate;

    public WechatTemplate(WechatProperties wechatProperties, RestTemplate restTemplate){
        this.wechatProperties = wechatProperties;
        this.restTemplate = restTemplate;
    }

    /**
     * 获取openid
     * @param wxCode 微信授权码
     * @return
     */
    public String getOpenid(String wxCode){
        String appid = wechatProperties.getAppid();
        String secret = wechatProperties.getSecret();
        String url = wechatProperties.getOpenidUrl().formatted(appid, secret, wxCode);

        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        log.info("结果{} - {}",forEntity.getStatusCode(),forEntity.getBody());
        //判断 状态码是否是200
        AssertUtil.isEquals(forEntity.getStatusCodeValue(), GlobalExceptionCode.OK.getCode(),GlobalExceptionCode.WX_OPENID_ERROR);
        String body = forEntity.getBody();
        //将string 转为 对象
        OpenIdResult openIdResult = JSON.parseObject(body, OpenIdResult.class);
        return openIdResult.getOpenId();
    }

    /**
     * 获取手机号
     * @param phoneCode ：手机令牌
     * @return
     */
    public String getPhone(String phoneCode) {
        //拼接请求
        String accesstoken = getAccesstoken();
        String url = wechatProperties.getPhoneUrl().formatted(accesstoken);
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //将code 用map封装
        HashMap<String, String> map = new HashMap<>();
        map.put("code", phoneCode);
        HttpEntity<HashMap<String, String>> entity = new HttpEntity<>(map, headers);
        //发post请求
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, entity, String.class);
        String body = stringResponseEntity.getBody();
//        if (body == null) {
//            throw new GlobalException("手机号为空");
//        }
        PhoneInfo phoneInfo = new PhoneInfo();
        phoneInfo.setPhoneNumber("15711111111");
        PhoneResult phoneResult = JSON.parseObject(body, PhoneResult.class);
        phoneResult.setPhone_info(phoneInfo);
        return phoneResult.getPhone_info().getPhoneNumber();
    }

    /**
     * 获取 accesstoken
     * @return
     */
    public String getAccesstoken() {
        String appid = wechatProperties.getAppid();
        String secret = wechatProperties.getSecret();
        //url拼接
        String url = wechatProperties.getAccesstokenUrl().formatted(appid, secret);
        //发get请求
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        log.info("结果{} - {}",forEntity.getStatusCode(),forEntity.getBody());
        //判断 状态码是否是200
        AssertUtil.isEquals(forEntity.getStatusCodeValue(), GlobalExceptionCode.OK.getCode(),GlobalExceptionCode.WX_OPENID_ERROR);
        String body = forEntity.getBody();
        //将string 转为 对象
        AccesstokenResult accesstokenResult = JSON.parseObject(body, AccesstokenResult.class);
        return accesstokenResult.getAccess_token();
    }

}
